load("//bazel/mkosi:mkosi_image.bzl", "mkosi_image")
load("//bazel/osimage:upload_os_images.bzl", "upload_os_images")
load(":variants.bzl", "CSPS", "STREAMS", "VARIANTS", "base_image", "constellation_packages", "images_for_csp", "images_for_csp_and_stream", "images_for_stream", "kernel_command_line", "kernel_command_line_dict")

[
    mkosi_image(
        name = variant["csp"] + "_" + variant["attestation_variant"] + "_" + stream,
        srcs = [
            "mkosi.postinst",
        ] + glob([
            "mkosi.repart/**",
        ]),
        base_trees = [
            base_image(
                variant["csp"],
                variant["attestation_variant"],
                stream,
            ),
        ],
        extra_trees = constellation_packages(stream),
        initrds = [
            "//image/initrd",
        ],
        kernel_command_line = kernel_command_line(
            variant["csp"],
            variant["attestation_variant"],
            stream,
        ),
        kernel_command_line_dict = kernel_command_line_dict(
            variant["csp"],
            variant["attestation_variant"],
            stream,
        ),
        mkosi_conf = "mkosi.conf",
        out_dir = variant["csp"] + "_" + variant["attestation_variant"] + "_" + stream,
        tags = [
            "manual",
            "no-cache",
        ],
        version_file = "//bazel/settings:tag",
        visibility = ["//visibility:public"],
    )
    for variant in VARIANTS
    for stream in STREAMS
]

[
    upload_os_images(
        name = "upload_" + variant["csp"] + "_" + variant["attestation_variant"] + "_" + stream,
        image_dirs = [":" + variant["csp"] + "_" + variant["attestation_variant"] + "_" + stream],
        tags = [
            "manual",
        ],
    )
    for variant in VARIANTS
    for stream in STREAMS
]

[
    filegroup(
        name = stream,
        srcs = images_for_stream(stream),
        tags = [
            "manual",
            "no-cache",
        ],
        visibility = ["//visibility:public"],
    )
    for stream in STREAMS
]

[
    upload_os_images(
        name = "upload_" + stream,
        image_dirs = [":" + stream],
        tags = [
            "manual",
        ],
    )
    for stream in STREAMS
]

[
    filegroup(
        name = csp,
        srcs = images_for_csp(csp),
        tags = [
            "manual",
            "no-cache",
        ],
        visibility = ["//visibility:public"],
    )
    for csp in CSPS
]

[
    filegroup(
        name = csp + "_" + stream,
        srcs = images_for_csp_and_stream(csp, stream),
        tags = [
            "manual",
            "no-cache",
        ],
        visibility = ["//visibility:public"],
    )
    for csp in CSPS
    for stream in STREAMS
]
